package vip.websky.project.user.service.impl;

import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.websky.project.common.error.CommonsRuntimeException;
import vip.websky.project.common.error.ErrorCode;
import vip.websky.project.common.error.ErrorMessage;
import vip.websky.project.common.utils.ObjectConvertUtils;
import vip.websky.project.core.log.KingLog;
import vip.websky.project.module.bo.user.UserRoleBO;
import vip.websky.project.module.dto.RequestDTO;
import vip.websky.project.module.dto.ResponseDTO;
import vip.websky.project.module.dto.request.user.AddUserRoleReqDTO;
import vip.websky.project.module.dto.request.user.EditUserRoleReqDTO;
import vip.websky.project.module.dto.request.user.GetUserRoleReqDTO;
import vip.websky.project.user.error.UserErrorCode;
import vip.websky.project.user.error.UserErrorMessage;
import vip.websky.project.user.server.UserRoleServer;
import vip.websky.project.user.service.UserRoleService;

import java.util.List;

/**
 * 用户-角色 接口实现类
 *
 * @Author YangYong E-mail:yang2048@qq.com
 * @Create 2017/9/25-2:08
 **/
@Slf4j
@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired private UserRoleServer userRoleServer;

    @Override
    @KingLog
    public ResponseDTO addUserRoleBatch(RequestDTO requestDTO) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            List<AddUserRoleReqDTO> addUserRoleReqDTOS = JSONArray.parseArray(requestDTO.getParam(), AddUserRoleReqDTO.class);
            // TODO: 校验参数
            //1.参数转换
            List<UserRoleBO> userRoleBOS = ObjectConvertUtils.copyToList(addUserRoleReqDTOS, UserRoleBO.class);
            //2.处理
            boolean result = userRoleServer.insertUserRoleBatch(userRoleBOS);
            //3.返回结果
            if (result) {
                responseDTO.setResult(result);
            } else {
                responseDTO.setStatusCode(UserErrorCode.USERROLE_ADD_FAIL);
                responseDTO.setStatusMessage(UserErrorMessage.USERROLE_ADD_FAIL);
                log.error("添加失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    @Override
    @KingLog
    public ResponseDTO editUserRoleBatch(RequestDTO requestDTO) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            List<EditUserRoleReqDTO> addUserRoleReqDTOS = JSONArray.parseArray(requestDTO.getParam(), EditUserRoleReqDTO.class);
            // TODO: 校验参数
            //1.参数转换
            List<UserRoleBO> userRoleBOS = ObjectConvertUtils.copyToList(addUserRoleReqDTOS, UserRoleBO.class);
            //2.处理
            boolean result = userRoleServer.updateUserRoleBatch(userRoleBOS);
            //3.返回结果
            if (result) {
                responseDTO.setResult(result);
            } else {
                responseDTO.setStatusCode(UserErrorCode.USERROLE_EDIT_FAIL);
                responseDTO.setStatusMessage(UserErrorMessage.USERROLE_EDIT_FAIL);
                log.error("修改失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    @Override
    @KingLog
    public ResponseDTO removeUserRoleBatch(RequestDTO requestDTO) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            List<GetUserRoleReqDTO> addUserRoleReqDTOS = JSONArray.parseArray(requestDTO.getParam(), GetUserRoleReqDTO.class);
            // TODO: 校验参数
            //1.参数转换
            List<UserRoleBO> userRoleBOS = ObjectConvertUtils.copyToList(addUserRoleReqDTOS, UserRoleBO.class);
            //2.处理
            boolean result = userRoleServer.delUserRoleBatch(userRoleBOS);
            //3.返回结果
            if (result) {
                responseDTO.setResult(result);
            } else {
                responseDTO.setStatusCode(UserErrorCode.USERROLE_NOT_EXIST);
                responseDTO.setStatusMessage(UserErrorMessage.USERROLE_NOT_EXIST);
                log.error("删除失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }
}
